package com.shujia.array.seven;

import org.junit.Test;

public class ArrayDemo {

    @Test
    public void test3() {
        // 方式1
        int[][] arr = {{3, 5, 8}, {12, 9}, {7, 0, 6, 4}};

        // 方式2
        int[][] arr1 = new int[3][];
        arr1[0] = new int[]{3, 5, 8};
        arr1[1] = new int[]{12, 9};
        // 报错
        arr1[2] = new int[]{7, 0, 6, 4};


    }

    @Test
    public void test2() {

        int[][] arr1 = new int[4][];
        arr1[0] = new int[3];
        arr1[0][2] = 5;
        arr1[1] = new int[]{1, 2, 3};
        System.out.println(arr1);
        // 在堆内存重新创建数组并赋值
        arr1 = new int[2][];
        System.out.println(arr1);

        System.out.println("------------------");
//
//        int[][] arr = new int[3][];
//        System.out.println(arr[0]);// null
//        System.out.println(arr[0][0]);// 空指针
//        arr[0] = new int[3];
//        arr[0][1] = 5;
//        arr[1] = new int[]{1, 2};
    }


    @Test
    public void test1() {
        // 静态初始化, 把数据的定义和初始化都做了
//        int[][] arr = new int[][]{{1, 2}, {3, 4, 5}, {6, 7, 8, 9}};
        int[][] arr = {{1, 2}, {3, 4, 5}, {6, 7, 8, 9}};

        System.out.println(arr[1][1]);

        // 二维数组的遍历
        for (int i = 0; i < arr.length; i++) {
            // arr[i] 获取的是一维数组
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j]);
                System.out.print(" ");
            }
            System.out.println();
        }
    }
}
